package com.example.qx.faxiana;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.example.qx.R;
import com.example.qx.database.data;
import com.example.qx.lei.cos;
import com.example.qx.lei.zhangmi;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.sql.SQLException;
import java.util.Map;
import java.util.Objects;

public class szbjtx extends AppCompatActivity {
    private Button butszbj,butsztx,butwc;
    private ImageView bj,tx;
    private static final  int  a=3;
    private String uri="s",d=null;
    private  data data1=new data();
    private zhangmi zhangmi= com.example.qx.lei.zhangmi.getInstance();

 private  cos cos=new cos();
    public szbjtx() throws SQLException, ClassNotFoundException, IllegalAccessException, InstantiationException {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.szbjtx);
        bj=findViewById(R.id.beijing);
        tx=findViewById(R.id.touxiang);
        butszbj=findViewById(R.id.szbeijing);
        butsztx=findViewById(R.id.sztouxiang);
        butwc=findViewById(R.id.szwc);
         butszbj.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 d="szbj";
                 selectFile();
             }
         });
         butsztx.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 d="sztx";
                 selectFile();
             }
         });
         butwc.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 Intent intent=new Intent(szbjtx.this,qxq.class);
                  startActivity(intent);
             }
         });

    }
    public void selectFile(){
        Intent intent =new Intent(Intent.ACTION_OPEN_DOCUMENT);
        intent.setType("*/*");
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        startActivityForResult(intent.createChooser(intent,"选择文件"),a);
    }
    @Override
    protected  void onActivityResult(int request,int result,Intent data){
        super.onActivityResult(request,result,data);
        if(request==a){
            if(result==RESULT_OK){
                Uri selectedFileUri=data.getData();
                uri=selectedFileUri.toString();
                ContentResolver contentResolver=getContentResolver();
                String  c=contentResolver.getType(selectedFileUri);


                try {
                    getApplicationContext().grantUriPermission(getApplication().getPackageName(),selectedFileUri,Intent.FLAG_GRANT_READ_URI_PERMISSION|Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);

                    getContentResolver().takePersistableUriPermission(selectedFileUri,Intent.FLAG_GRANT_READ_URI_PERMISSION|Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
                }catch (SecurityException e){
                    e.printStackTrace();
                }


                if(c.startsWith("image/")){
                    String a=String.valueOf(zhangmi.getZhang());
                    try {
                        InputStream inputStream=getContentResolver().openInputStream(selectedFileUri);
                        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                        if(Objects.equals(d, "szbj"))
                        {     a=a+"bj";
                            bj.setImageBitmap(bitmap);
                            cos.delete(a,getApplication());
                            File file=getApplication().getDir("youqishuju",getApplication().MODE_PRIVATE);
                            File fiell=new File(file,a);
                            if(fiell.exists())
                                fiell.delete();
                            cos.insert(selectedFileUri,a,getApplication());
                            cos.get(a,getApplication());
                            data1.updatebjtx(uri,zhangmi.getZhang(),1);

                        }
                        if(Objects.equals(d, "sztx"))
                        {     a=a+"tx";
                            tx.setImageBitmap(bitmap);
                            cos.delete(a,getApplication());
                            File file=getApplication().getDir("youqishuju",getApplication().MODE_PRIVATE);
                              File fiell=new File(file,a);
                              if(fiell.exists())
                                  fiell.delete();
                            cos.insert(selectedFileUri,a,getApplication());
                            cos.get(a,getApplication());
                            data1.updatebjtx(uri,zhangmi.getZhang(),2);
                        }
                        inputStream.close();
                    }  catch (IOException | SQLException e) {
                        throw new RuntimeException(e);
                    }
                    final boolean tupian[]={false};
                    bj.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if(!tupian[0])
                            {   butszbj.setVisibility(View.GONE);
                                butwc.setVisibility(View.GONE);
                                butsztx.setVisibility(View.GONE);
                                tx.setVisibility(View.GONE);
                                getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN|View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY|View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
                                getSupportActionBar().hide();
                                ConstraintLayout.LayoutParams params2=(ConstraintLayout.LayoutParams) bj.getLayoutParams();
                                params2.height= ViewGroup.LayoutParams.MATCH_PARENT;
                                params2.width=ViewGroup.LayoutParams.MATCH_PARENT;
                                tupian[0]=true;

                            }
                            else{
                                butszbj.setVisibility(View.VISIBLE);
                                butwc.setVisibility(View.VISIBLE);
                                butsztx.setVisibility(View.VISIBLE);
                                tx.setVisibility(View.VISIBLE);
                                getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
                                getSupportActionBar().show();
                                ConstraintLayout.LayoutParams params3=(ConstraintLayout.LayoutParams) bj.getLayoutParams();
                                params3.width=(int) (250*getApplicationContext().getResources().getDisplayMetrics().density);
                                params3.height=(int) (150*getApplicationContext().getResources().getDisplayMetrics().density);
                                tupian[0]=false;
                            }

                        }
                    });
                    final boolean tupian1[]={false};
                    tx.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if(!tupian1[0])
                            {   butszbj.setVisibility(View.GONE);
                                butwc.setVisibility(View.GONE);
                                butsztx.setVisibility(View.GONE);
                                bj.setVisibility(View.GONE);
                                getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN|View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY|View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
                                getSupportActionBar().hide();
                                ConstraintLayout.LayoutParams params2=(ConstraintLayout.LayoutParams) tx.getLayoutParams();
                                params2.height= ViewGroup.LayoutParams.MATCH_PARENT;
                                params2.width=ViewGroup.LayoutParams.MATCH_PARENT;
                                tupian1[0]=true;

                            }
                            else{
                                butszbj.setVisibility(View.VISIBLE);
                                butwc.setVisibility(View.VISIBLE);
                                butsztx.setVisibility(View.VISIBLE);
                               bj.setVisibility(View.VISIBLE);
                                getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
                                getSupportActionBar().show();
                                ConstraintLayout.LayoutParams params3=(ConstraintLayout.LayoutParams) tx.getLayoutParams();
                                params3.width=(int) (250*getApplicationContext().getResources().getDisplayMetrics().density);
                                params3.height=(int) (150*getApplicationContext().getResources().getDisplayMetrics().density);
                                tupian1[0]=false;
                            }

                        }
                    });

                }
            }
}}



}